/***
 * author: Adam Pociejowski
 */

package requests.music;

import java.io.FileInputStream;

import requests.IRequest;
import main.DemiGui;
import javazoom.jl.player.Player;

public class Mp3Player extends Thread implements IRequest {
	
	private String path = "";
	private DemiGui instance = null;
	private Mp3Random mp3Random;
	private Player playMP3;
	public boolean isPlaylist = false;
	private String title;
	
	public Mp3Player(String path, DemiGui instance, String title) {
		
		this.instance = instance;
		this.path = path;
		this.title = title;
		isPlaylist = false;
		
		start();	
	}
	
	public Mp3Player(String path, DemiGui instance, Mp3Random mp3Random, String title) {
		
		this.instance = instance;
		this.path = path;
		this.mp3Random = mp3Random;
		this.title = title;
		isPlaylist = true;
		
		start();	
	}
	
	public void run() {
		
	/*************      Opening and playing a .mp3 file      *************/
		
		 try {
			 FileInputStream stream = new FileInputStream(path);
			 playMP3 = new Player(stream);

			 returnDEMI("Currently Playing: "+title);
			 playMP3.play();
			 
			if (isPlaylist == true) {
				 mp3Random.next();			
			}
		 }
		 catch(Exception e) {
			 returnDEMI("Mp3 Player error..");
		 }
		 
	/*********************************************************************/
		
	}
	
	@SuppressWarnings("deprecation")
	public void exit() {
		playMP3.close();
		Thread.currentThread().stop();		
	}
	
	public void singleExit() {
		playMP3.close();
		returnDEMI("Mp3 Player - terminated..");
	}

	@Override
	public void returnDEMI(String text) {
		instance.Monitor(text);
		
	}

}
